package com.yh.jandan.ext.thread ;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.alibaba.druid.filter.stat.StatFilter;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.yh.jandan.model.Config;
import com.yh.jandan.model.Meizi;
import com.yh.jandan.util.DateUtils;

public class MeiziUpdateThread {

	private final static String _CONFIG_ = getD ( "config" , "a_little_config.txt" ) ;
	static String update_file = getD ( "update" , "update.sql" ) ;

	// static Timer timer = null ;

	static DruidPlugin dp ;
	static ActiveRecordPlugin arp ;

	public static void main ( String[] args ) {
		init ( ) ;
		start ( ) ;
		stop ( ) ;
	}

	private static void init ( ) {
		Prop p = PropKit.use (new File(_CONFIG_)  ) ;
		dp = new DruidPlugin ( p.get ( "jdbcUrl" ).trim ( ) , p.get ( "user" ) , p.get ( "password" ) ) ;
		dp.addFilter ( new StatFilter ( ) ) ;
		dp.setMinIdle ( 5 )//
				.setTimeBetweenEvictionRunsMillis ( 6 * 1000 )//
				.setValidationQuery ( "select 1" )//
				.setTestWhileIdle ( true )//
				.setTestOnBorrow ( true )//
				.setTestOnReturn ( true ) ; //
		dp.start ( ) ;
		arp = new ActiveRecordPlugin ( dp ) ;
		arp.setDialect ( new MysqlDialect ( ) ) ;
		arp.addMapping ( "config" , "id" , Config.class ) ;
		arp.addMapping ( "meizi" , "id" , Meizi.class ) ;

		arp.start ( ) ;
	}

	public static void stop ( ) {
		// if ( null != timer ) timer.cancel ( ) ;
		// timer = null ;
		arp.stop ( ) ;
		dp.stop ( ) ;
	}

	public static void start ( ) {
		// if ( null != timer ) timer.cancel ( ) ;
		// timer = new Timer ( ) ;
		// timer.schedule ( new TimerTask ( ) {
		// public void run ( ) {
		try {
			exec_sql ( ) ;
		} catch ( IOException e ) {
			e.printStackTrace ( ) ;
		}
		// }
		// } , new Date ( new Date ( ).getTime ( ) + 100 * 1000 ) , 1 * 60 *
		// 1000 ) ;

	}

	protected static void exec_sql ( ) throws IOException {
		if ( !new File ( update_file ).exists ( ) ) throw new FileNotFoundException ( update_file ) ;
		List<String> sqls = FileUtils.readLines ( new File ( update_file ) ) ;
		for ( String sql : sqls ) {
			if ( StringUtils.isNotBlank ( sql ) ) {
				write_log ( "update():[" + new Date ( ) + "]" + sql ) ;
				update ( sql.trim ( ) ) ;
			}
		}
	}

	private static void update ( String sql ) {
		try {
			Db.update ( sql ) ;
		} catch ( Exception e ) { 
			e.printStackTrace ( ) ;
		}
	}

	private static void write_log ( String string ) {
		try {
			System.out.println ( string ) ;
			FileUtils.write ( new File ( getLogFile ( ) ) , string + "\r\n" , true ) ;
		} catch ( IOException e ) {
			e.printStackTrace ( ) ;
		}
	}

	private static String getLogFile ( ) {
		String datestr = DateUtils.getDateByFormat ( DateUtils.YMD_FORMAT ) ;
		String log_file = getD ( "log" , "MeiziUpdateThread-{date}.log" ) ;
		return log_file.replace ( "{date}" , datestr ) ;
	}

	private static String getD ( String k , String d ) {
		String v = System.getProperty ( k ) ;
		boolean b = null == v || "".equals ( v ) ;
		return b ? d : v ;
	}
}
